<template>
  <view class="container">
    <backView />
    <view class="header text-center">
      <text class="title">新增就诊信息</text>
    </view>
    
    <FormView
      :field-config="fieldConfig"
      :form="form"
      v-if="form.name"
      :show-buttons="true"
      @submit="handleSubmit"
    />
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import Taro from '@tarojs/taro'
import { request } from '@/service/request'
import backView from '@/components/backView.vue'
import FormView from '@/components/FormView.vue'
import { healthSeeDocFields } from './fields'
import { localStg } from '@/utils'

const fieldConfig = healthSeeDocFields

const form = reactive({
  name: '',
  idNumber: '',
  recordid: '',
})

onMounted(() => {
  const obj = localStg.get("demographicInfo");
  if(obj){
    form.name = obj.name;
    form.idNumber = obj.idNumber;
    form.recordid = obj.recordid;
  }
})

async function handleSubmit(formData: any) {
  try {
    await request.post('/health_records/health_seedoc/healthSeeDocInfo/add', formData, {
      useErrMsg: true
    })
    
    Taro.showToast({ title: '添加成功', icon: 'success' })
    
    setTimeout(() => {
      Taro.navigateBack()
    }, 1500)
  } catch (error) {
    console.error('添加失败:', error)
    Taro.showToast({ title: '添加失败', icon: 'error' })
  }
}
</script>

<style scoped>
.container {
  padding: 32rpx;
  background: #f6f8fa;
  min-height: 100vh;
}

.header {
  margin-bottom: 40rpx;
}

.title {
  font-size: 48rpx;
  font-weight: bold;
  color: #2563eb;
}
</style>
